//
//  NewsSimpleView.m
//  Pink
//
//  Created by 杨成阳 on 15/12/13.
//  Copyright © 2015年 ycy. All rights reserved.
//

#import "NewsSimpleView.h"

@implementation NewsSimpleView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/



-(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [super touchesEnded:touches withEvent:event];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"pushToDetail" object:self.newsObject];
}

-(void)initSubViews
{
    self.layer.shadowColor = [UIColor blackColor].CGColor;
    self.layer.shadowOpacity = 0.33;
    self.layer.shadowOffset = CGSizeMake(0, 1.5);
    self.layer.shadowRadius = 4.0;
    self.layer.shouldRasterize = YES;
    self.layer.rasterizationScale = [UIScreen mainScreen].scale;
    self.headImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"defaultHead"]];
    [self addSubview:self.headImageView];
    
    _headImageView.frame = CGRectMake(10,10,60,60);
    _headImageView.contentMode = UIViewContentModeScaleAspectFill;
    _headImageView.userInteractionEnabled = YES;
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithActionBlock:^(id sender) {
        
        NSLog(@"2");
        
    }];
    [_headImageView addGestureRecognizer:tap];
    
    self.nickName = [[UILabel alloc] initWithFrame:CGRectMake(73, 10, self.size.width - 80 , 20)];
    self.nickName.font = [UIFont boldSystemFontOfSize:17];
    self.nickName.textColor = [UIColor blackColor];
    [self addSubview:self.nickName];
    
    UIImageView *pushStar = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"pushCount"]];
    pushStar.frame = CGRectMake(73, 30, 18, 15);
    [self addSubview:pushStar];
    self.userPushCount = [[UILabel alloc] initWithFrame:CGRectMake(95, 30, 100, 15)];
    _userPushCount.font = [UIFont systemFontOfSize:15];
    _userPushCount.textColor = [UIColor lightGrayColor];
    [self addSubview:_userPushCount];
    
    _NewsDate = [[UILabel alloc] initWithFrame:CGRectMake(73, CGRectGetMaxY(_userPushCount.frame) + 2, 150, 20)];
    _NewsDate.textColor = [UIColor lightGrayColor];
    _NewsDate.font = [UIFont systemFontOfSize:12];
    [self addSubview:_NewsDate];
    
    
    _NewsTitle = [YYLabel new];
    _NewsTitle.frame = CGRectMake(10, CGRectGetMaxY(_headImageView.frame) + 15, 0, 0);
    if (_newsObject.title) {
        
        NSMutableAttributedString *titleText = [[NSMutableAttributedString alloc] initWithString:_newsObject.title];
        titleText.font = [UIFont boldSystemFontOfSize:18];
        titleText.color = [UIColor blackColor];
        YYTextContainer *container = [YYTextContainer new];
        container.size = CGSizeMake(self.size.width - 20, CGFLOAT_MAX);
        container.maximumNumberOfRows = 3;
        
        YYTextLayout *layout = [YYTextLayout layoutWithContainer:container text:titleText];
        
        // 创建属性字符串
        
        _NewsTitle.size = layout.textBoundingSize;
        _NewsTitle.textLayout = layout;
  
    }
    
    [self addSubview:_NewsTitle];
    
    
    
    NSArray *contentArr = [_newsObject.content objectFromJSONString];
    
    
    
    
    NSString *firstText = nil;
    NSString *firstImage = nil;
    
    for (int i = 0; i < contentArr.count; i ++) {
        
        NSDictionary *paragraph = contentArr[i];
        if ([paragraph[@"content"] length] > 0) {
            firstText = paragraph[@"content"];
             break;
        }
    }
    
    for (int i = 0; i < contentArr.count; i ++) {
        NSDictionary *paragraph = contentArr[i];
        if ([paragraph[@"imagePath"] length]>0) {
            firstImage = paragraph[@"imagePath"];
            break;
            
        }
    }
    
    
    _FirstParagraph = [YYLabel new];
    _FirstParagraph.frame = CGRectMake(10, CGRectGetMaxY(_NewsTitle.frame)+ 5, 0, 0);
    
    if (firstText) {
        NSMutableAttributedString *paragText = [[NSMutableAttributedString alloc] initWithString:firstText];
        paragText.font = [UIFont systemFontOfSize:14];
        paragText.color = [UIColor blackColor];
        YYTextContainer *container1 = [YYTextContainer new];
        container1.size = CGSizeMake(self.size.width - 20, CGFLOAT_MAX);
        container1.maximumNumberOfRows = 3;
        
        YYTextLayout *layout1 = [YYTextLayout layoutWithContainer:container1 text:paragText];
        
        // 创建属性字符串
        
        _FirstParagraph.size = layout1.textBoundingSize;
        _FirstParagraph.textLayout = layout1;

    }
       [self addSubview:_FirstParagraph];
    

    _FirstImage = [[UIImageView alloc] initWithFrame:CGRectMake(10, CGRectGetMaxY(_FirstParagraph.frame) + 5, self.size.width - 20, self.size.height - CGRectGetMaxY(_FirstParagraph.frame) - 50 )];
    _FirstImage.contentMode = UIViewContentModeScaleAspectFit;
    if (firstImage) {
        [_FirstImage setImageWithURL:[NSURL URLWithString:firstImage] options:YYWebImageOptionProgressiveBlur |YYWebImageOptionSetImageWithFadeAnimation ];
         [self addSubview:_FirstImage];
    }
   
    NSArray *btnImages = @[@"dislike",@"communicate",@"like"];
    
    for (int i = 0 ; i < 3; i ++) {
        
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(self.size.width / 3 * i, self.size.height - self.size.width / 3 * 0.4, self.size.width / 3, self.size.width / 3 * 0.4);
        
        [button setBackgroundImage:[UIImage imageNamed:btnImages[i]] forState:UIControlStateNormal];
        button.tag = 50 + i;
        [button addTarget:self action:@selector(bottomButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:button];
    }
    
    
    
    
    
}

- (void)bottomButtonClicked:(UIButton *)sender
{
    NSInteger index = sender.tag - 50;
    switch (index) {
        case 0:
            [[NSNotificationCenter defaultCenter] postNotificationName:@"swipeToLeft" object:nil];
            break;
        case 1:
            [[NSNotificationCenter defaultCenter] postNotificationName:@"chatRoom" object:nil];
            break;
        case 2:
            [[NSNotificationCenter defaultCenter] postNotificationName:@"swipeToRight" object:nil];
            break;
            
        default:
            break;
    }
}


-(void)initDatas
{
    WS(weakself)
    
    AVQuery *userQuery = [AVUser query];
    userQuery.cachePolicy = kAVCachePolicyNetworkElseCache;
    userQuery.maxCacheAge = 24 * 3600 * 3;
    [userQuery getObjectInBackgroundWithId:_newsObject.userId ?_newsObject.userId : @""  block:^(AVObject *object, NSError *error) {
        if (object) {
            [weakself.headImageView setImageWithURL:[NSURL URLWithString:((AVFile *)object[@"avatar"]).url] options:YYWebImageOptionProgressiveBlur |YYWebImageOptionSetImageWithFadeAnimation];
            weakself.nickName.text = object[@"nickName"]?object[@"nickName"] : ((AVUser *)object).username;
            weakself.userPushCount.text = object[@"pushCount"]?[object[@"pushCount"] description]:@"0";
            
        }else{
            weakself.nickName.text = @"";
            weakself.userPushCount.text = @"0";
        }
    }];
    
    
    self.NewsDate.text = [Commen simpleDateWithDate:_newsObject.createdAt];
    
}

@end
